﻿var page = (function () {
    function cinit(p) {
        p.id = webjs.newid();
        p.name = "page_" + p.id;
        p.gridname = p.name + "_grid";
        p.textname = p.name + "_text";
        var top = '<div class="panel-header" style="padding:0;border-width:1px 0;text-align:center;height:25px"><span><input id="' + p.textname + '" class="textbox" style="width:200px;height:20px"/></span></div>';
        var grid = '<table id="' + p.gridname + '"></table>';
        if (p.applyTo != "") {
            $("#" + p.applyTo).attr("id", p.id);
            if (!p.show) {
                $("#" + p.id).panel({
                    fit: true
                });
            }
            $("#" + p.id).append(top);
            $("#" + p.textname).textbox({
                iconCls: 'icon-search',
                iconAlign: 'left',
                buttonText: '查询',
                height: '25px',
                onClickButton: function () {
                    var txt = $(this).textbox('getText');
                    var where = [];
                    if (txt != "" && txt != null) {
                        for (var i in p.searchWhere) {
                            where.push(p.searchWhere[i]);
                            where[i].Value = txt;
                        }
                    }
                    if (typeof p.defultWhere == 'object' && p.defultWhere.length > 0) {
                        for (var i in p.defultWhere)
                            where.push(p.defultWhere[i]);
                    }
                    else
                        where.push(p.defultWhere);
                    $("#" + p.gridname).datagrid('loading');
                    p.datasource.load(where);
                }
            });
            $("#" + p.id).append(grid);
            if (p.c != "") {
                p.datasource = new datasource();
                p.datasource.c = p.c;
                p.datasource.init();
                $("#" + p.gridname).datagrid({
                    singleSelect: true,
                    border: false,
                    fit: true,
                    fitColumns: true,
                    scrollbarSize: 0,
                    showHeader: false,
                    columns: [[{
                        field: 'ROWINDEX', width: 80, formatter: function (value, row) {
                            var s = "";
                            var maintext = farmattext(p.datasource, p.mainColumns, row);
                            var subtext = farmattext(p.datasource, p.subColumns, row);
                            var mark = "";
                            if (row.REMARK && row.REMARK != "") {
                                mark = row.REMARK;
                            }
                            if (mark != "")
                                mark = "</br>" + mark;
                            s += '<div style="font-size:16px;font-weight:bold">' + maintext + '</div>';
                            s += '<div style="line-height:24px">' + subtext + mark + '</div>';
                            return s;
                        }
                    },
                         {
                             field: 'CODE', width: 20, fixed: true, formatter: function (value, row) {
                                 return formatArrow();
                             }
                         }
                        ]],
                    onSelect: function (index, row) {
                        openwin(p, index, row);
                    }
                });
                p.datasource.onLoad = function (data) {
                    $("#" + p.gridname).datagrid('loadData', data);
                    $("#" + p.gridname).datagrid('loaded');
                }
                if (p.isonline) {
                    $("#" + p.gridname).datagrid('loading');
                    p.datasource.load(p.defultWhere);
                }
            }
        }
    }
    function openwin(p, rowIndex, rowData) {
        p.datasource.selectItem = rowData;
        try {
            if (!p.f) {
                var item1 = p.datasource.getitem(p.whname);
                var wname = item1.foreign.displayname;
                var f = new from();
                p.f = f;
                if (p.editmodel)
                    p.f.editmodel = p.editmodel;
                if (p.cztit != "")
                    p.f.cztitle = p.cztit;
                p.f.editindex = p.editindex;
                p.f.showdiv = p.fromedit;
                f.parent = p;
                f.datasource = p.datasource;
                f.onTabSelect = function (title, index) {
                    if (p.onTabSelect)
                        return p.onTabSelect(f, title, index);
                }
                var id = webjs.newid() + "_form";
                $("body").append("<div id='" + id + "'></div>")
                f.applyTo = id;
                f.title = p.title + '&nbsp;&nbsp;' + rowData.CODE + '&nbsp;&nbsp;' + rowData[wname];
                if (p.onFromShowing) {
                    p.onFromShowing(p.f, rowData);
                }
                f.init();
                f.onClose = function () {
                    if (p.f.isupdate) {
                        $("#" + p.gridname).datagrid('loading');
                        p.datasource.reload(function () {
                            $("#" + p.gridname).datagrid('loaded');
                        });
                    }
                    if (p.onClose)
                        p.onClose(f);
                    f.map = null;
                    $("#" + f.id).panel("destroy");
                    p.f = null;
                }
            }
            p.f.show();
            if (p.onFromShow) {
                p.onFromShow(p.f, rowData);
            }
            if (p.onDblClick)
                p.onDblClick(p.f, rowData)
        }
        finally {

        }
    }
    function farmattext(data, cols, row) {
        var text = "";
        for (var i in cols) {
            var n = cols[i];
            var item = data.getitem(n);
            var vvv = "";
            if (item && item.foreign && item.foreign.isfkey) {
                var fn = item.foreign.displayname;
                vvv = row[fn];
            }
            if (item && item.comvtp && item.comvtp.isvtp) {
                for (var v in item.comvtp.items) {
                    var com = item.comvtp.items[v];
                    if (com.Value == row[n]) {
                        vvv = com.Text;
                        break;
                    }
                }
            }
            if (vvv == "")
                vvv = row[n];
            if (vvv != null) {
                if (text == "")
                    text = vvv;
                else
                    text += "&nbsp;" + vvv;
            }
        }
        return text;
    }
    function formatArrow() {
        return '<span class="arrow">&nbsp;</span>';
    }
    function loading(p, txt) {
        var text = "操作中请稍等。。。";
        if (txt)
            text = txt;
        var _1c7 = $("#" + p.id).panel();
        if (!_1c7.children("div.datagrid-mask").length) {
            $("<div class=\"datagrid-mask\" style=\"display:block\"></div>").appendTo(_1c7);
            var msg = $("<div class=\"datagrid-mask-msg\" style=\"display:block;left:50%\"></div>").html(text).appendTo(_1c7);
            msg._outerHeight(40);
            msg.css({ marginLeft: (-msg.outerWidth() / 2), lineHeight: (msg.height() + "px") });
        }
    }
    function loaded(p) {
        var _1c8 = $("#" + p.id).panel();
        _1c8.children("div.datagrid-mask-msg").remove();
        _1c8.children("div.datagrid-mask").remove();
    }
    return {
        c: "",
        applyTo: "",
        id: "",
        f: null,
        datasource: null,
        title: "",
        show: false,
        searchWhere: [],
        defultWhere: "",
        mainColumns: ['ROWINDEX', 'CODE', 'CLIENTNO'],
        subColumns: ['STATE', 'SOURCECODE', 'TOWHNO', 'CREATEDBY'],
        fromedit: 2,
        editindex: 1,
        cztit: "",
        isonline: true,
        whname: "TOWHNO",
        childWhere: [{ ColumnName: "STAYNUM", Value: "0", Symbol: ">"}],
        init: function () {
            cinit(this);
            // window.page = this;
        },
        load: function (data) {
            $("#" + this.gridname).datagrid('loadData', data);
        },
        onDblClick: function () {

        },
        keydown: function (key) {
            if (key.keyCode == "50")
                grid.selectRow(-1);
            if (key.keyCode == "56")
                grid.selectRow(1);
        }
    }
})